﻿

//物品记录
// owen  
//2013-12-18

var mStockBillList_Query = {};//查询条件
//列表模式
var mStockBillList_ModeType = 0; //0:列表显示，1：查询
//单据类型
var mStockBillList_BillType = 0;


var mStockBill_User; //当前用户

$(function () {

    var args = getUrlArgs();
    mStockBillList_ModeType = parseInt(args["modeType"]);
    mStockBillList_BillType = parseInt(args["billType"]);

    var tbt = parseInt(args["targetBillType"]);
    mStockBillList_Query.TargetBillType = isNaN(tbt) ? 0 : tbt;//查询的源单类型
    mStockBillList_Query.CheckState = -1;
    mStockBill_User = gGetLoginUser();
    $("#cmbOrg").combobox({
        valueField: 'Id',
        textField: 'Name',
        editable: false,
        panelHeight: 120,
        onSelect: function (record) { mStockBillList_Query.DeptId = record.Id; }
    });

    $("#cmbManageDept").combobox({
        valueField: 'Id',
        textField: 'Name',
        editable: false,
        panelHeight: 120,
        onSelect: function (record) { mStockBillList_Query.ManageDeptId = record.Id; }
    });

    gLoadAllDepts(function (orgs) {
        var allOrg = {};
        allOrg.Id = 0;
        allOrg.Name = "全部";
        orgs.splice(0, 0, allOrg);
        $('#cmbOrg').combobox('loadData', orgs);
        $('#cmbManageDept').combobox('loadData', orgs);
    }, false);


    $("#cmbCheckState").combobox({
        valueField: 'Id',
        textField: 'Name',
        editable: false,
        panelHeight: 65,
        onSelect: function (record) { mStockBillList_Query.CheckState = record.Id; }
    });

    $("#cmbConfirmState").combobox({
        valueField: 'Id',
        textField: 'Name',
        editable: false,
        panelHeight: 65,
        data: [{ 'Id': 0, 'Name': '全部' }, { 'Id': 1, 'Name': '已确认' }, { 'Id': 2, 'Name': '未确认' }],
        onSelect: function (record) { mStockBillList_Query.ConfirmState = record.Id; }
    });

    packQuery();//打包初始查询条件

    $('#stockBillDataGrid').datagrid({
        url: '/Case/LoadStockBillSimpleList',
        fitColumns: true,
        rownumbers: true,
        resizable: true,
        pagination: true,
        pageNumber: 1,
        pageSize: 20,
        nowrap: false,
        height: 540,
        idField: 'Id',
        toolbar: '#stockBillToolbar',
        queryParams: { 'stockBillList_Query': JSON.stringify(mStockBillList_Query) },
        singleSelect: true,
        onDblClickRow: selStockRow,
        columns: [[
               { title: 'Id', field: 'Id', align: 'left', width: 10, hidden: true },
               { title: '状态', field: 'IsCheck', align: 'left', width: 50, formatter: getCheckState },
               { title: '案件编号', field: 'Number', align: 'left', width: 100 },
               { title: '单据编号', field: 'SerialNo', align: 'left', width: 100 },
               { title: '记录日期', field: 'BillTime', align: 'center', width: 100, formatter: getFormatDate },
               { title: '办案部门', field: 'TransferDeptName', align: 'left', width: 150, formatter: gFormatDeptNameForColumn },
               { title: '办案人', field: 'TransferName', align: 'left', width: 80 },
               { title: '保管部门', field: 'ManageDeptName', align: 'left', width: 200, formatter: gFormatDeptNameForColumn }

        ]]
    });

    setting();

    //loadStockBills();


});
//查询
function searchStockBillsAction() {
    loadStockBills();
}

function selStockRow(rowIndex, rowData) {
    if (window.parent.onSelectSourceBill) {
        window.parent.onSelectSourceBill(mStockBillList_BillType, rowData.Id); //sourceType =物品单 =1; 
        window.parent.mStockBill_Query_Dlg.close();
    } else {
        editStockBillAction();
    }
}

function modeSetting() {

    if (mEvidenceBillList_ModeType == 1) {
        $('#btnAddStockBill').hide();
        $('#btnEditStockBill').hide();
        $('#btnCheckStockBill').hide();
        $('#btnUnCheckStockBill').hide();
        $('#btnDelStockBill').hide();
    }
}

function getStockStates() {
    var stats = [];
    switch (mStockBillList_BillType) {
        case 2:
        case 3:
            stats = [{ Id: 0, Name: '全部' }, { Id: 1, Name: '未入库' }, { Id: 2, Name: '已入库' }];
            break;
        case 4:
        case 5:
            stats = [{ Id: 0, Name: '全部' }, { Id: 3, Name: '未出库' }, { Id: 4, Name: '已出库' }];
            break;
    }


    var x = stats[0].Name;

    return stats;
}

//状态
function getCheckState(value, rowData, index) {
    if (value) {
        return '已审核'
    }
    else {
        return '未审核'
    }
}

function getFormatDate(value, rowData, index) {
    if (value) {
        return gFormatDateFormCSharp(value, 'yyyy-MM-dd');
    }
}
//打包参数
function packQuery() {
    mStockBillList_Query.Number = $('#txtCaseNo').val();
    mStockBillList_Query.BillType = mStockBillList_BillType;
    mStockBillList_Query.BeginTime = $('#dteBeginTime').datebox('getValue');
    mStockBillList_Query.EndTime = $('#dteEndTime').datebox('getValue');
    mStockBillList_Query.TransferName = $('#txtTransferName').val();
    mStockBillList_Query.EvidenceName = $('#txtEvidenceName').val();
    mStockBillList_Query.SerialNo = $('#txtSerialNo').val();
}

//读取所有物品单据
function loadStockBills() {
    try {
        packQuery();
        var json = JSON.stringify(mStockBillList_Query);
        $('#stockBillDataGrid').datagrid('reload', { 'stockBillList_Query': json });

    } catch (ex) {
        alert(ex);
    }
}

function getStockBillSelected() {
    var row = $("#stockBillDataGrid").datagrid("getSelected");
    var id = 0;
    if (row) {
        id = row.Id;
    }
    return id;
}

function addStockBillAction() {
    try {
        mStorageViewDlg = art.dialog({
            id: 'diaforstokcbillView',
            title: gGetBillTypeName(mStockBillList_BillType),
            content: "<iframe scrolling='yes' frameborder='0' src='/Case/StockBillView?billType=" + mStockBillList_BillType + "&stockBillId=0' style='width:810px;height:480px;'/>",
            lock: true,
            initFn: function () {
            }
        });
    } catch (ex) {
        alert(ex);
    }
}

function editStockBillAction() {
    try {

        var billId = getStockBillSelected();

        if (billId == 0) {
            $.messager.alert("提示", "请选择要查看的数据", "warning");
            return;
        }
        mStorageViewDlg = art.dialog({
            id:'diaforstokcbillView',
            title: gGetBillTypeName(mStockBillList_BillType),
            content: "<iframe scrolling='yes' frameborder='0' src='/Case/StockBillView?billType=" + mStockBillList_BillType + "&stockBillId=" + billId + "' style='width:810px;height:480px;'/>",
            lock: true,
            initFn: function () {
            }
        });
    } catch (ex) {
        alert(ex);
    }
}

function checkBillAction() {
    try {
        var row = $("#stockBillDataGrid").datagrid("getSelected");
        var billId = 0;
        var deptId = 0;
        if (row) {
            billId = row.Id;
            deptId = row.TransferDeptId;

            if (row.IsCheck) {
                $.messager.alert("提示", "该条记录已审核，请勿重复审核", "warning");
                return;
            }

        }
        else {
            $.messager.alert("提示", "请选择要审核的数据", "warning");
            return;
        }
        $('#div_stockBillDataGrid').mask("正在审核，请稍后……");
        $.ajax({
            url: "/Case/CheckStockBill",
            type: "POST",
            async: false,
            dataType: "json",
            data: { "checkDeptId": deptId, "checkerId": mStockBill_User.Id, "billId": billId },
            success: function (req) {
                if (req.IsSuccess) {
                    $.messager.alert('提示', '审核成功!');
                    loadStockBills();
                } else {
                    $.messager.alert('提示', req.Data, "warning");
                }
                $('#div_stockBillDataGrid').unmask();
            },
        });

    } catch (ex) {
        alert(ex);
        $('#div_stockBillDataGrid').unmask();
    }
};

function delStockBillAction() {
    try {
        var row = $("#stockBillDataGrid").datagrid("getSelected");
        var billId = 0;
        var deptId = 0;
        if (row) {
            billId = row.Id;
            deptId = row.TransferDeptId;
        }
        else if (row.IsCheck) {
            $.messager.alert("提示", "该条记录已审核，不能删除", "warning");
            return;
        } else {
            $.messager.alert("提示", "请选择要删除的数据", "warning");
            return;
        }
        $('#div_stockBillDataGrid').mask("正在删除，请稍后……");
        $.ajax({
            url: "/Case/DeleteStockBill",
            type: "POST",
            async: false,
            dataType: "json",
            data: { "checkDeptId": deptId, "billId": billId },
            success: function (req) {
                if (req.IsSuccess) {
                    $.messager.alert('提示', '删除成功!');
                    loadStockBills();
                } else {
                    $.messager.alert('提示', req.Data, "warning");
                }
                $('#div_stockBillDataGrid').unmask();
            },
        });
    } catch (ex) {
        alert(ex);
        $('#div_stockBillDataGrid').unmask();
    }
};

function searchStockBillListAction() {
    loadEvidenceBills();
}

function setting() {
    if (mStockBillList_ModeType == 1) {
        $('#btnAddStockBill').hide();
        $('#btnEditStockBill').hide();
        $('#btnCheckStockBill').hide();
        $('#btnUnCheckStockBill').hide();
        $('#btnDelStockBill').hide();
    }
}


